Descriptive Statistics

Welcome to the notebook on descriptive statistics. Statistics is a very large field. People even go to grad school for it. For our site here, we will focus on some of the big hitters in statistics that make you a good data scientist. This is a cursory, whirlwind overview by somebody who has no letters after his name. So any mistakes or corrections, blame someone who does. And then send me an email or submit a pull request on Github and we'll square it away in no time.

Descriptive statistics are measurements that describe a population (or sample of that population) of values. They tell you where the center tends to be, how spread out the values are, the shape of the distribution, and a bunch of other things that gradute students have put in their theses. But here we focus on some of the simpler values that you have to know to consider yourself a functional human, let alone data scientist. So follow along as we take a \$5 hop-on hop-off tour of some of our basic statistics.

Requirements

We'll use two 3rd party Python libraries for displaying graphs. Run from terminal or shell:

> pip3 install seaborn
> pip3 install pandas

In [2]:
import seaborn as sns
import random as random
%matplotlib inline

Useful Python functions

Many statistics require knowing the length or sum of your data. Let's chug through some useful built-in functions.

Random

We'll use the random module a lot to generate random numbers to make fake datasets. The plain vanilla random generator will pull from a uniform distribution. There are also options to pull from other datasets. As we add more tools to our data science toolbox, we'll find that NumPy's random number generators to be more full-featured and play really nicely with Pandas, another key data science library. For now, we're going to avoid the overhead of learning another library and just use Python's standard library.


In [3]:
random.seed(42)
values = [random.randrange(1,1001,1) for _ in range(10000)]
values[0:10]


Out[3]:
[655, 115, 26, 760, 282, 251, 229, 143, 755, 105]

len() & sum()

The building blocks of average. Self explanatory here.


In [4]:
len(values)


Out[4]:
10000

In [5]:
sum(values)


Out[5]:
5021696

Below we'll use Seaborn to plot and visualize some of our data. Don't worry about this too much. Visualization, while important, is not the focus of this notebook.


In [6]:
sns.stripplot(x=values, jitter=True, alpha=0.2)


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee2549bf60>

This graph is pretty cluttered. That makes sense because it's 10,000 values between 1 and 1,000. That tells us there should be an average of 10 entries for each value. I'll leave counting this average as an exercise for the reader.

Let's make a sparse number line with just 200 values between 1 and 1000. There should be a lot more white space.


In [7]:
sparse_values = [random.randrange(1,1001) for _ in range (200)]
sns.stripplot(x=sparse_values, jitter=True)


Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee25ea5240>

max() & min()

These built-in functions are useful for getting the range of our data, and just general inspection:


In [8]:
print("Max value: {}\nMin value: {}".format(
    max(values), min(values)))


Max value: 1000
Min value: 1

sorted()

Another very important technique in wrangling data is sorting it. If we have a dataset of salaries, for example, and we want to see the 10 top earners, this is how we'd do it. Let's look now at the first 20 items in our sorted data set. Probably won't be too exciting though:


In [9]:
sorted_vals = sorted(values)
sorted_vals[0:20]


Out[9]:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3]

If we wanted to sort values in-place (as in, perform an action like values = sorted(values)), we would use the list class' own sort() method:

values.sort()

The Mean

The mean is a fancy statistical way to say "average." You're all familiar with what average means. But mathemeticians like to be special and specific. There's not just one type of mean. In fact, we'll talk about 3 kinds of "means" that are all useful for different types of numbers.

  1. Arithmetic mean for common numbers
  2. Geometric mean for returns or growth
  3. Harmonic mean for ratios and rates

Buckle up...

1. Arithmetic mean

This is your typical average. You've used it all your life. It's simply the sum of the elements divided by the length. Intuitively, think of it as if you made every element the exact same value so that the sum of all values remains the same as before. What would that value be?

Mathematically the mean, denoted $\mu$, looks like:

$$\mu = \frac{x_1 + x_2 + \cdots + x_n}{n}$$

where $\bar{x}$ is our mean, $x_i$ is a value at the $i$th index of our list, and $n$ is the length of that list.

In Python, it's a simple operation combining two builtins we saw above: sum() and len()


In [10]:
def arithmetic_mean(vals):
    return sum(vals) / len(vals)

arithmetic_mean(values)


Out[10]:
502.1696

From this we see our average value is 502.1696. Let's double check that with our intuitive definition using the sum:


In [11]:
avg_sum = len(values) * arithmetic_mean(values) #10,000 * 502.1696
print("{} =? {}".format(sum(values), avg_sum))


5021696 =? 5021696.0

2. Geometric mean

The geometric mean is a similar idea but instead uses the product. It says if I multiply each value in our list together, what one value could I use instead to get the same result?

The geometric mean is very useful in things like growth or returns (e.g. stocks) because adding returns doesn't give us the ability to get returns over a longer length of time. In other words, if I have a stock growing at 5% per year, what will be the total returns after 5 years?

If you said 25%, you are wrong. It would be $1.05^5 - 1 \approx 27.63\%$

Mathematically, our geometric is: $$ GM(x) = \sqrt[n]{x_1 \times x_2 \times \cdots \times x_n }$$


In [12]:
returns = [1.05, 1.06, .98, 1.08]

def product(vals):
    ''' 
    This is a function that will multiply every item in the list
    together reducing it to a single number. The Pythonic way to
    do this would be to use the 'reduce' function like so:
    > reduce(lambda x, y: x * y, vals)
    We are explicit here for clairty.
    '''
    prod = 1
    for x in vals:
        prod = prod * x
    return prod

def geometric_mean(vals):
    geo_mean = product(vals) ** (1/len(vals)) # raising to 1/n is the same as nth root
    return geo_mean

geom = geometric_mean(returns)
geom


Out[12]:
1.041804547674296

Using our product function above, we can easily multiply all the values together to get what your return after 4 years is:


In [13]:
product(returns)


Out[13]:
1.1779992000000004

or roughly $17.8\%$. Using our geometric mean should give us the same result:


In [14]:
geom**4


Out[14]:
1.1779992

Now look at what happens with the arithmetic mean:


In [15]:
arm = arithmetic_mean(returns)
arm


Out[15]:
1.0425

In [16]:
arm**4


Out[16]:
1.1811478250390623

The arithmetic mean would tell us that after 4 years, we should have an $18.1\%$ return. But we know it should actually be a $17.8\%$ return. It can be tricky to know when to use the arithmetic and geometric means. You also must remember to add the $1$ to your returns or it will not mathematically play nice.

3. Harmonic mean

This one is also a bit tricky to get intuitively. Here we want an average of rates. Not to be confused with an average of returns. Recall a rate is simply a ratio between two quantities, like the price-to-earnings ratio of a stock or miles-per-hour of a car.

Let's take a look at the mph example. If I have a car who goes 60mph for 50 miles, 50mph for another 50, and 40mph for yet another 50, then the car has traveled 150 miles in $\frac{50mi}{60\frac{mi}{h}} + \frac{50mi}{50\frac{mi}{h}} + \frac{50mi}{40\frac{mi}{h}} = 3.08\bar{3}h$. This corresponds to a geometric mean of $150mi \div 3.08\bar{3}h \approx 48.648mph$. Much different from our arithmetic mean of 50mph.

(Note: if in our example the car did not travel a clean 50 miles for every segment, we have to use a weighted harmonic mean.)

Mathematically, the harmonic mean looks like this:

$$ \frac{n}{\frac{1}{x_1}+\frac{1}{x_2}+\cdots+\frac{1}{x_n}} $$

So let's code that up:


In [17]:
speeds = [60, 50, 40]

def harmonic_mean(vals):
    sum_recip = sum(1/x for x in vals)
    return len(vals) / sum_recip

harmonic_mean(speeds)


Out[17]:
48.648648648648646

Now you know about the three Pythagorean means. Thank me after you brag at your next party. Let's now move on to something very important in descriptive statistics:

The Median

The median should be another familiar statistic, but often misquoted. When somebody is describing a set of numbers with just a mean, they might not be telling the whole story. For example, many sets of values are skewed (a concept we will cover in the histogram section) in that most values are clustered around a certain area but have a long tail. Prices are usually good examples of this. Most wine is around \$15-20, but we've all seen those super expensive bottles from a hermit's chateau in France. Salaries are also skewed (and politicians like to remind us how far skewed just 1\% of these people are).

A useful statistic in these cases is the "median." The median gives us the middle value, as opposed to the average value. Here's a simple, but illustrative example:

Suppose we take the salaries of 5 people at a bar

[12000, 48000, 72000, 160000, 3360000]

If I told you the average salary in this bar right now is \$730,400, I'd be telling you the truth. But you can tell that our rich friend pulling in over 3 million is throwing off the curve. When he goes home early to do a business, the average drops to just \\$73,000. A full 10 times less.

The median instead in this case is much more consistent, or in other words, not as prone to outliers. To find the median, we simply take the middle value. Or if there are an even number of entries, we take the average of the two middle values. Here it is in Python:


In [18]:
salaries = [12000, 48000, 72000, 160000, 3360000]

def median(vals):
    n = len(vals)
    sorted_vals = sorted(vals)
    midpoint = n // 2
    
    if n % 2 == 1:
        return sorted_vals[midpoint]
    else:
        return arithmetic_mean([sorted_vals[midpoint-1], sorted_vals[midpoint]])
    
median(salaries)


Out[18]:
72000

A much more reasonable \$7200! Now let's see what happens when Moneybags goes home:


In [19]:
median(salaries[:-1])


Out[19]:
60000.0

The median drops down to \$60,000 (which is the average of \\$48,000 and \$72,000).

Let's take a look at our original values list of 10,000 numbers.


In [20]:
median(values)


Out[20]:
506.0

In [21]:
# Recall our values list is even, meaning 506.0 was both item 5000 and 5001
len(values)


Out[21]:
10000

In [22]:
# Lopping off the end returns the same value
median(values[:-1])


Out[22]:
506

In [23]:
# Why? There are 9 506s in the list
from collections import Counter
c = Counter(values)
c[506]


Out[23]:
9

Above we used the Counter class in the standard library. This class is a subclass of the dict that holds a dictionary of keys to their counts. We can build our own version of it like so:


In [24]:
# Here we use the defaultdict that will initialize our first value if it doesn't yet exist
from collections import defaultdict
def make_counter(values):
    counts = defaultdict(int)
    for v in values:
        counts[v] += 1
    return counts

counts = make_counter([1, 2, 2, 3, 5, 6, 6, 6])
counts


Out[24]:
defaultdict(int, {1: 1, 2: 2, 3: 1, 5: 1, 6: 3})

Remember this part because it will show up very soon when we talk about histograms, the chef's knife of a data scientist's data exploration kitchen.

But first, there's one more descriptive statistic that we should cover. And that is

The Mode

The mode is simply the most common element. If there are multiple elements with the same count, then there are two modes. If all elements have the same count, there are no modes. If the distribution is continuous (meaning it can take uncountably infinite values, which we will discuss in the Distributions chapter), then we use ranges of values to determine the mode. Honestly, I don't really find the mode too useful. A good time to use it is if there's a lot of categorical data (meaning values like "blue", "red", "green" instead of numerical data like 1,2,3). You might want to know what color car your dealership has the most of.

Let's take a look at that example now. I've built a set of cars with up to 20 cars of any of four colors.


In [28]:
car_colors = ["red"] * random.randint(1,20) + \
    ["green"] * random.randint(1,20) + \
    ["blue"] * random.randint(1,20) + \
    ["black"] * random.randint(1,20)
car_colors


Out[28]:
['red',
 'red',
 'green',
 'green',
 'green',
 'blue',
 'blue',
 'blue',
 'blue',
 'blue',
 'blue',
 'blue',
 'black',
 'black',
 'black',
 'black',
 'black']

In [30]:
#Using our familiar counter
color_count = Counter(car_colors)
color_count


Out[30]:
Counter({'red': 2, 'green': 3, 'blue': 7, 'black': 5})

In [31]:
# We can see the mode above is 'blue' because we have 18. Let's verify:
def mode(counter):
    # store a list of name:count tuples in case multiple modes
    modes = [('',0)]
    for k,v in counter.items():
        highest_count = modes[0][1]
        if v > highest_count:
            modes = [(k,v)]
        elif v == highest_count:
            modes.append((k,v))
        
    return modes
mode(color_count)


Out[31]:
[('blue', 7)]

In [32]:
# If we have multiple modes?
mode(Counter(['blue']*3 + ['green']*3 + ['black']*2))


Out[32]:
[('blue', 3), ('green', 3)]

But that's enough about modes. Check out wikipedia if you want more because there's no point spending more time on them then they're worth.

Hang in there, because we're getting close. Still to come is Percentiles, Boxplots, and Histograms. Three very import things.

Let's get to it.

Percentiles

A percentile is familiar to anyone who has taken the SAT. It answers the question: what percentage of students are dumber than I am? Well the College Board would love to tell you: congratulations, you're in the 92nd percentile!

Let's take a look at our old friend Mr. values with 10,000 numbers from 1-1000. Since this list is uniformly distributed, meaning every value is as likely to occur as any other, we expect that 25% of the numbers to be below 250, 50% to be below 500, and 75% to be below 750. Let's verify:


In [33]:
def percentile(vals, elem):
    '''Returns the percent of numbers
    below the index.
    '''
    count = 0
    sorted_val = sorted(values)
    for val in sorted_val:
        if val > elem:
            return count/len(values)
        count += 1

for num in [250, 500, 750]:
    print("Percentile for {}: {}%".format(num, percentile(values, num)*100))


Percentile for 250: 24.37%
Percentile for 500: 49.45%
Percentile for 750: 75.48%

Just like we'd expect. Now if the data set is not so nice and uniform, we expect these values to be quite different. Let's write a function to give us an element at a particular percentile:


In [34]:
from math import ceil
def pct_value(vals, pct):
    sorted_vals = sorted(vals)
    n = len(vals)
    return sorted_vals[ceil(n*pct)]

for pct in [.25, .5, .75]:
    print("Element at percentile {}%: {}".format(pct*100, pct_value(values, pct)))


Element at percentile 25.0%: 256
Element at percentile 50.0%: 506
Element at percentile 75.0%: 745

Notice how the element at the 50th percentile is also our median! Now we have a second definition of the median.

Let's take a look now at a highly skewed set. It will range from 0-100 but we'll cluster it around 10


In [35]:
skewed = []
for i in range(1,100):
    skewed += [i]*random.randint(0,int(4+i//abs(10.1-i)))

In [49]:
def print_statistics(vals, calc_mode=True):
    print("Count: {}".format(len(vals)))
    print("Mean: {:.2f}".format(arithmetic_mean(vals)))
    print("Median: {}".format(median(vals)))
    if calc_mode: print("Mode: {}".format(mode(Counter(vals))))
    print("Max: {}".format(max(vals)))
    print("Min: {}".format(min(vals)))
    print("Range: {}".format(max(vals)-min(vals)))
    for pct in [.25, .5, .75]:
        print("{:.0f}th Percentile: {}".format(pct*100, pct_value(vals, pct)))
    print("IQR: {}".format(pct_value(vals, 0.75) - pct_value(vals, 0.25)))

print_statistics(skewed)


Count: 353
Mean: 39.37
Median: 30
Mode: [(10, 73)]
Max: 99
Min: 1
Range: 98
25th Percentile: 10
50th Percentile: 32
75th Percentile: 66
IQR: 56

A few clues that this distribution is skewed:

  • The mean is significantly different from the median
  • The percentiles cluster around 25. A uniform distribution we'd expect 25, 50, and 75 for our percentiles.
  • The max i smuch higher than the mean, median, or even 75th percentile.

Let's take a look at a simple plot to describe all of these statistics to us:

The Boxplot

Also sometimes the Box-and-Whisker plot, this is a great way to visualize a lot of the information our print_statistics function displayed. In particular, we can see in one graph

  • Median
  • 75th percentile (called the third quartile)
  • 25th percentile (called the first quartile)
  • The reach ($\pm1.5*IQR$), which shows outliers

It does not show the mean, but it can be intuited by looking at the plot. Let's take a look at plots for values and skewed:


In [50]:
sns.boxplot(values)


Out[50]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee23416b70>

A classic uniform distribution: centered about 500, the median goes right down the middle, and the whiskers are evenly spaced. Now let's take a look at the skewed list:


In [51]:
sns.boxplot(skewed)


Out[51]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee232e9048>

Looks pretty different? Instead of being centered around 50, it looks like the box is centered around 40. The median is at 27 and much of the box is to the right of it. This shows us that the distribution is skewed to the right.

There's another important way to visualize a distribution and that is

Histogram

Ah, the moment we've all been waiting for. I keep teaching you ways to describe a dataset, but sometimes a picture is worth a thousand words. That picture is the Histogram.

A histogram is a bar chart in which the values of the dataset are plotted horizontally on the X axis and the frequencies (i.e. how many times that value was seen) are plotted on the Y axis. If you remember our functions to make a counter, a histogram is essentially a chart of those counts.

Think for a minute on what the histogram for our uniform dataset of 10,000 randomly generated numbers would look like? Pretty boring right?


In [52]:
# Seaborn gives an easy way to plot histograms. Plotting from scratch is beyond the
# scope of the programming we will do
sns.distplot(values, kde=False, bins=100)


Out[52]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee23257ef0>

Seaborn's helpful sns.distplot() method simply turns a dataset into a histogram for us. The bins parameter allows us to make bins where instead of the frequency count of each variable, we plot the frequency count of a range of variables. This is very useful when we have continuous distribution (i.e. one that can take an infinite number of values of the range), as plotting every individual value is unfeasible and would make for an ugly graph.

Let's take a look at our skewed data:


In [53]:
sns.distplot(skewed, kde=False, bins=100)


Out[53]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee231c63c8>

The data has tons of values around 10, and everything else hovers around 4. This type of distribution is called "unimodal" in that it has one peak, or one "contender" for the mode. Practically, unimodal and bimodal are incredibly common.

I'm going to jump ahead to the next notebooks where we generate and describe different types of these distributions, how they're used, and how to describe them. One of the most basic and fundamental distribution in statistics is the unimodal Normal (or Gaussian) distribution. It's the familiar bell-curve. Let's use a simple, albeit slow, function to generate numbers according to the normal distribution (taken from https://www.taygeta.com/random/gaussian.html)


In [54]:
from math import sqrt, log, cos, sin, pi

def generateGauss():
    x1 = random.random() # generate a random float in [0,1.0)
    x2 = random.random()
    y1 = sqrt(-2 *log(x1)) * cos(2*pi*x2)
    y2 = sqrt(-2*log(x1)) * sin(2*pi*x2)
    return y1, y2

gaussValues = []
for _ in range(10000):
    gaussValues += list(generateGauss())

In [55]:
#Let's take a peek:
gaussValues


Out[55]:
[-0.885320982302321,
 -0.47188093578604334,
 0.047955459881986705,
 0.5746934606014507,
 -0.9949409228291612,
 0.6240862352894445,
 -0.737497940821195,
 0.8159414648985025,
 -0.19085374583120554,
 -0.08711326444119062,
 0.6336776194126372,
 0.5458029565386199,
 -0.4415302083425239,
 -1.1254387957138152,
 -0.2316635552123139,
 -1.9618758135950882,
 0.181016500910358,
 -0.018640409906697582,
 -0.6898932607586278,
 0.15449923407619964,
 1.7386317193410077,
 -1.439877162467125,
 -2.169523559856579,
 0.3793227617997027,
 0.7492898393014542,
 -0.1903235409249126,
 -1.5073686196574534,
 -1.6816162998031756,
 -0.25700927845784616,
 0.6204783572860695,
 -1.3449988539073687,
 2.0185244702901306,
 -0.694110065359546,
 0.1375284963852512,
 -1.1344392650330999,
 0.22208165797042825,
 -0.6918524976872877,
 0.37461714634861243,
 0.08462300587477241,
 -0.27068848442544163,
 1.536544154600503,
 0.34676685110505784,
 -0.0014468883533416083,
 -0.40067891137193024,
 -0.7354574233047259,
 -0.31118772205635564,
 -2.3287165227124187,
 0.3964741149640296,
 1.7871099123637524,
 0.6008441946601507,
 0.05032704799444483,
 -0.12973421540702404,
 -0.22253716135429918,
 0.9448128110028045,
 -2.232375689028342,
 -0.07852579995914302,
 -0.09873087396562322,
 0.5584732391047466,
 -0.38876650078525954,
 1.9724656807112781,
 0.5515405768901688,
 0.04935063644502689,
 0.9553695876740644,
 1.8638251923771065,
 0.5961640665571674,
 0.832984062211629,
 0.4513215532198715,
 -1.1866990558499775,
 -0.4987178348691309,
 -1.5607099861878213,
 0.36727320282901166,
 -1.14571341293239,
 -0.30018316754296814,
 0.4548149152553265,
 1.362076743302796,
 1.7575801455331985,
 0.20943486305963055,
 0.3046624881044754,
 1.1833386149763858,
 -0.047717962433073656,
 0.0546494043888004,
 0.7202693263415101,
 -0.8315262418614369,
 -0.9745175435818657,
 -0.173095932761272,
 -1.7559015901194186,
 -0.9137873049426319,
 -1.1370908406894937,
 0.5586242583940162,
 0.4987308597364578,
 -1.5202855100577415,
 1.122977032385816,
 -0.037457472052743634,
 -1.548369804352245,
 -0.7385634933215259,
 -0.1530499686109917,
 -0.38310475251063225,
 -1.3377023315649141,
 0.7473929574798563,
 0.9654084860794533,
 0.5256850050062793,
 0.039256067698273694,
 -0.3688190057886453,
 0.12286547951384595,
 0.8554754756448025,
 -0.20957754956630695,
 0.8445851917512266,
 0.22261832587341224,
 -0.26929369098906286,
 -0.3149982963786325,
 -0.27227328820052565,
 0.12440983864891485,
 0.2603010420222047,
 -0.3213527770298021,
 -0.1584893908789852,
 1.4704846834001981,
 -1.218097095553957,
 -1.7765624912957072,
 0.19860270660134144,
 -1.3556791812583115,
 -0.07479778283026894,
 -0.7564358150649722,
 0.3491375333994845,
 0.9683478640489526,
 -0.5402500182551064,
 -1.3715184996685932,
 -1.7699028294845462,
 -0.5458636545755478,
 0.4371559139636625,
 -0.7755665794643075,
 2.432065196623584,
 1.713825544535345,
 -2.0064550091503257,
 0.7089787276178845,
 -0.36173403111723157,
 1.7037362286953215,
 1.7935723314293575,
 0.26908080901924314,
 2.0237038594260066,
 -0.8233061195229378,
 0.46919285516264475,
 -0.7881635636386303,
 -0.20607775951483245,
 -0.24308316012505998,
 0.6057885916460163,
 0.5687094167505249,
 0.3813393106551413,
 0.4056111901996017,
 0.7420039505717666,
 -0.539829919722217,
 1.0441857483709684,
 -0.21853012273096564,
 -0.15693487569950465,
 -0.44158525520745034,
 -1.6215403277069176,
 0.3415936814983309,
 -0.23362895858810032,
 -0.27804340603820576,
 -0.06619373694922889,
 0.39563951849069046,
 1.0715555843268783,
 -0.7441699534984301,
 -0.32185494120633634,
 0.6476490677891363,
 1.7055292547602394,
 0.8555157830345546,
 -0.5261690177720252,
 0.47697308546096795,
 1.3792875274429062,
 0.5466865853596286,
 -0.027728930759066475,
 0.30008483043280115,
 -1.2786394404335126,
 0.06656661731617997,
 -0.29614052693487836,
 -0.2459363858983418,
 -1.854293297326277,
 0.09261631851892568,
 0.2309274499398767,
 -1.9555885800520296,
 -1.0834969215588695,
 -1.3671181872449172,
 -0.09224155736906535,
 -0.417596101445723,
 1.9110768118580954,
 0.9424811348623007,
 -0.003964580935634858,
 1.0580689581160154,
 1.0116318472682115,
 -0.7694170798835139,
 -1.240961777018586,
 -1.5065154514753958,
 -0.19052920693858674,
 -0.3203538780819165,
 -0.5020546071109359,
 -1.546990623311861,
 -0.3994125969353873,
 -0.20237498475535878,
 1.0702947166244574,
 -0.4244515742232178,
 -1.098267745300628,
 0.4666223046292223,
 0.11248046095793734,
 -0.5713468649978845,
 -0.673126903328519,
 -0.173410016228358,
 3.2405735975563066,
 -0.9601523641984759,
 -0.6298907466578125,
 0.07127833025208132,
 0.9773944333437562,
 -0.4226487794645677,
 -0.5945416228237708,
 -1.3022446377361285,
 -0.911741442262022,
 -0.9991182981752463,
 0.2441955501719211,
 0.15641055001680615,
 1.0855370717845427,
 -0.27199970644151367,
 -2.1732082174238356,
 0.5632372447017407,
 -0.4367896294013375,
 -0.5839819115413957,
 1.8055580325306106,
 0.4555094778849784,
 0.5010908254860881,
 1.2892948243955458,
 0.6776238742007141,
 -0.5157280590436372,
 -0.6086557360305777,
 0.7427322265544227,
 -1.4918434768713373,
 -1.6455544234239812,
 0.5000510974652862,
 0.6040169892110497,
 -0.3427566515550836,
 -1.7809232074892685,
 -1.0200630869544813,
 0.06911670866912589,
 -2.2755652969166817,
 -0.5926867973610426,
 -1.500494628795137,
 -0.3249233136357373,
 -0.5717521662839468,
 0.188528730899179,
 0.08891527641469928,
 0.021076637257313815,
 0.5644923805529524,
 -2.333283463396998,
 1.8494067532076999,
 1.6974834302334587,
 -0.32502274116034235,
 -0.5683013637447211,
 -0.5454352418331957,
 -1.1236796371432192,
 0.30747398699763795,
 0.4174029420897598,
 0.9304186947245675,
 -0.4754291128664425,
 1.6177455485617298,
 1.995794979499027,
 -0.906820600559697,
 0.16579363211049397,
 0.3966019182782492,
 0.1173930834150939,
 0.5745886069729524,
 0.6160325264886929,
 1.7838918417030811,
 0.5791011503425795,
 0.4164696564917627,
 -1.4785708210875501,
 0.5988196257373528,
 1.1005137620647434,
 0.4237870522734691,
 -0.9338839729124667,
 -2.116825287743523,
 0.037996734552444796,
 0.1231017221644104,
 0.6581763262431493,
 -0.5900461676989193,
 0.2950079173265313,
 0.6422482964512842,
 -0.07216010759904518,
 -0.3961574572999253,
 0.7868303717704138,
 -0.1963897847820464,
 -0.10157331133521583,
 1.0296915581376829,
 -1.706013184361071,
 0.06078785883058653,
 -0.678168525383685,
 -0.5231095968349441,
 0.8781745834397192,
 1.2983478192019702,
 1.594501057348008,
 -0.43898585770277265,
 -1.2511999605346242,
 -1.0080115870305757,
 0.4089414723758099,
 0.3802375745842577,
 0.09941024192680122,
 1.2325569655264816,
 -1.7568769457588997,
 0.17124417817359147,
 -0.9753353868008431,
 -2.8742722001951146,
 0.23588946508677683,
 1.3650111096160487,
 2.8732348226699513,
 -1.2727649741594538,
 -1.7930799540998765,
 -0.4235889557830987,
 0.24613885552755702,
 0.09241821711055004,
 -0.6475351548885285,
 0.23322524281369497,
 0.8251722623965536,
 -0.935124205418405,
 -0.13660774398043599,
 1.232170591410566,
 -0.5050420161013056,
 0.9657685125923213,
 0.2326385693342674,
 -1.8519152135082815,
 0.0013074722160040203,
 -0.22265608787017266,
 -0.22887431121377572,
 -0.4039028738686462,
 0.5719903625051269,
 0.29581011328169704,
 3.2693978740247305,
 -2.0322787728208658,
 -0.3903123521343207,
 -1.400775747983073,
 1.652645970192068,
 -0.7159615116150563,
 -0.1824991953607786,
 0.22193776630043183,
 -1.727886149648659,
 -0.150856635973027,
 0.9001739372301469,
 0.5230885134380154,
 -1.7346676126401521,
 0.8525656507267881,
 -0.17056707628971998,
 0.5453481848701375,
 1.092680232146389,
 0.8620921432403832,
 0.23942961089650597,
 -0.20470937785891052,
 0.914076340343599,
 -0.6521722117544132,
 1.114001868399533,
 0.034764679736988165,
 -0.6574463145101995,
 -0.1507816663935344,
 -2.501132309003751,
 0.17747309912521192,
 0.3531659631654687,
 -0.6918440188009624,
 0.06512925209400765,
 1.6405056741505273,
 -1.2712000080181216,
 -0.6748851792493068,
 -0.4445722386425499,
 0.473846222157567,
 1.420336372477313,
 -0.40449293189666535,
 0.7854865613392961,
 -0.3253477926683853,
 0.6274557750858987,
 -0.16573764352728376,
 -2.1486735929725866,
 -0.3746002113252921,
 0.37210241326353266,
 -1.7096598154501863,
 0.315123408235942,
 0.9300638596889443,
 -0.7646161744217131,
 1.7670941353731189,
 -0.860367128523198,
 -0.9534189713742959,
 0.8160564355654438,
 -0.37622681596523605,
 -1.9602838541305205,
 0.9466720070243644,
 -0.14655052461391022,
 1.2466045517930757,
 1.2817430773754643,
 0.7982022505546236,
 0.3140118464045256,
 1.1897350711630201,
 -1.602301413844407,
 0.593288161256985,
 1.0987515129595486,
 1.104984595455721,
 -0.9569236520470275,
 0.24710553100055527,
 -0.9739131053746422,
 -0.7246645332120628,
 -0.6113082445924998,
 1.8496822667265123,
 -0.3512098390599994,
 0.4791235032141212,
 0.28937671154764444,
 -0.5543285783977443,
 0.03762681324192284,
 -0.07820916989656759,
 -0.05072970950209857,
 -0.10873068134573487,
 0.5865716305293645,
 -0.06880686439522327,
 -0.7903407257759576,
 -0.7810349810325504,
 1.4797028049187082,
 0.8124891439675112,
 -0.6566234950096772,
 0.6074429727358405,
 0.824792086116485,
 1.0536431453908275,
 0.03362619101156904,
 0.19377055417609734,
 0.04982798166600118,
 0.14320157712298612,
 -1.2193290173124305,
 -2.5552057411112874,
 -0.5061214700893337,
 0.36039187187888183,
 -1.1580599242923788,
 -0.5996909932364908,
 0.18473089778109245,
 -1.5754734545752787,
 -0.6065131517488735,
 -0.33828954574479414,
 -2.9431229883052223,
 0.2153903327611843,
 0.23734524499571816,
 -0.9360574675790684,
 -1.2490014386716775,
 -2.17318025979359,
 0.2775480189811552,
 -0.44578524825167454,
 -1.4755454127622336,
 -0.009437588549253997,
 -0.553529455453237,
 -2.615970723071899,
 0.7628481960651412,
 0.506337624149625,
 -0.5114598272440349,
 0.33304855665635313,
 -1.3019355813127844,
 -1.3740226194513372,
 0.3135202039600309,
 0.6109335689228926,
 -0.7348871972429358,
 1.8048775636830787,
 -1.4286623898725328,
 1.4519465632159168,
 0.30272193259250907,
 -0.16203944073884852,
 1.2964698390824776,
 -0.32113859365039765,
 -0.08771285339230431,
 0.15914906138256266,
 -0.6594009762938863,
 1.642667749968808,
 -1.5261934631272898,
 0.7373904143053718,
 0.26449166991342066,
 -0.15289635137575738,
 -0.29846533659979807,
 0.37370015601406087,
 0.31299365385778766,
 -1.0394010568056262,
 -0.9654668965347903,
 0.641932304353444,
 1.2651132982419457,
 0.9846596030342497,
 0.4409160025320706,
 0.23265441136242637,
 0.7954373876108046,
 0.013884050724444815,
 0.1940617077351669,
 -2.0668633157022134,
 0.2810067783935127,
 -0.31039189439249343,
 -0.1709427129861177,
 0.07222981740660522,
 0.27981233281720447,
 0.11794454710083382,
 0.7581466441284934,
 -0.157139423763513,
 1.4351013603908576,
 -0.1490041200249086,
 0.8651334959502769,
 -1.3230689536116658,
 1.9408873881100406,
 -1.184062156191583,
 -0.20039104847416528,
 -0.5115897337280261,
 -1.9834686857026753,
 -0.8623750968904836,
 -0.18059890736941964,
 -1.1369495424069882,
 -1.0805271272421135,
 0.6292399351270778,
 -0.2249101000924414,
 0.6070351803479623,
 -0.23648617009164927,
 1.816033438489813,
 -1.466131200580325,
 1.0946755977163771,
 0.603330171360586,
 -0.11078965727530514,
 0.4505736967344077,
 0.23021285844181943,
 0.18890641247133233,
 -0.5237810186413316,
 -1.5168313251986454,
 0.8369327376280853,
 -0.8370048936466492,
 -0.7962039262035902,
 -0.8193102117717501,
 1.5379901263854343,
 -0.2976498699543215,
 0.7479455819009101,
 0.8170936756372931,
 -1.3659207069016805,
 0.50284197461445,
 -0.2416637721201929,
 0.9053105880692303,
 1.6291191894574042,
 -1.6961177188529657,
 1.616274142870914,
 -0.5539562922415127,
 -0.7964535677312302,
 0.4936485135272979,
 -0.8892131366048295,
 -1.1621359923531844,
 -1.246432465346843,
 -0.8283978201699388,
 0.46396882010386326,
 -0.7120843740239894,
 -0.4239966139291902,
 1.199348685724778,
 0.49252894343068987,
 -1.951413647822495,
 -0.11795314275117766,
 1.8734107076764692,
 1.1839820544734336,
 -0.2202791273431276,
 0.6917098596916093,
 0.2119366352818016,
 -1.3879041873843612,
 0.3944763218044795,
 1.6828332594885773,
 -0.8690440788238709,
 -0.18111648140804051,
 0.8044123511424004,
 0.12405450246927711,
 -0.19730510060055534,
 -0.42977467934413316,
 -0.7410468784003185,
 0.14698520176759194,
 1.9304669972765827,
 -1.0268501609051266,
 2.3079364512020324,
 -0.13288171443321725,
 2.0292331598678817,
 -1.6986507271385611,
 -0.7034446519016778,
 -1.0587129128583457,
 2.3668094868656375,
 -1.0196027718906178,
 1.0466742930014403,
 0.5795730394000231,
 0.9470805244275406,
 1.3589111632184463,
 -0.7422496442083504,
 -1.3453158094252495,
 0.004928755814862367,
 -1.896007644297458,
 0.5117715262938274,
 1.5085918216224972,
 0.04071458353877069,
 0.7695092319975061,
 -0.11890116570790249,
 -0.4106607393946187,
 -1.4286135013031183,
 -0.30263278469762267,
 1.1840127752825298,
 0.3369717270335949,
 -0.041086485839393366,
 0.6351281264125769,
 0.8413164934793755,
 0.8905164655799093,
 -0.5899323403303336,
 -0.4766979346960184,
 -1.185399679212241,
 0.6400582595123646,
 -0.46395837961539377,
 -1.8841742677082105,
 -0.6247949441246674,
 -1.2506314580690363,
 0.8892121502075624,
 -0.5551467667432053,
 0.1960409013772244,
 2.05292691795555,
 0.7395893129992288,
 -0.8160164193391253,
 -0.47179822975115293,
 1.5948081182645932,
 -0.6822369606535871,
 1.5842139843134353,
 0.22596108941038598,
 -1.0480662641878542,
 0.3739230381415093,
 0.7612242232747899,
 0.46566955449886716,
 0.3958331231090269,
 1.0876615336504771,
 -1.1960326686589329,
 -0.9286750896592486,
 0.286419487823943,
 -0.18571242657961817,
 0.8538223556530276,
 1.1438455633144275,
 -0.38392172371150574,
 0.4107446720758774,
 -0.303419456735654,
 1.3715669555675432,
 1.4319467908960042,
 -0.20357931870394771,
 0.44177568375991405,
 0.2710972153863551,
 -0.7486107975041677,
 0.27366629591022773,
 0.040345014329175075,
 -0.11503487342084784,
 0.5402683210959086,
 -1.4809799705377455,
 -0.21774596908119667,
 0.11296720786817298,
 0.2990527460122018,
 0.35949994988402717,
 0.8846339858827049,
 0.5312910739316075,
 -1.750209547697908,
 0.28156873851756165,
 0.5312451995413122,
 -0.04470510124368866,
 2.2579342265039912,
 -0.5782980687512042,
 0.635220265945267,
 0.3944886730168452,
 1.1477243300545807,
 -0.6845128657248964,
 -1.2463641139272637,
 -0.4559314498399386,
 0.027249690873876603,
 -0.8267662302714821,
 0.7803818209345095,
 1.835107166979045,
 -1.3090297708376086,
 -0.46077912212496946,
 1.1596828390257088,
 -2.2325426111867377,
 -0.1998412052214321,
 0.5671898948718587,
 -1.0497562821788897,
 0.2453936488625892,
 2.6896120503279386,
 0.04135042721243394,
 1.573117803345393,
 -0.4460498563334678,
 -0.5931091505085125,
 0.10335602951861977,
 0.7201403977954616,
 -0.5755568562376128,
 -1.3206233484062508,
 0.06738191495439681,
 0.028530042465783986,
 0.8646240948489777,
 -0.35088378635188633,
 2.0317953326186577,
 0.09737995782093972,
 0.04339899697099287,
 -0.6690316140997045,
 -0.37365904006081035,
 0.7227128111216833,
 -0.7858195570328194,
 -0.9536852075986667,
 0.14466284082073083,
 0.6242991612974345,
 0.5831903485440307,
 -1.029314425026609,
 -0.5266118106707113,
 0.30186830120755437,
 0.49502531128029637,
 -1.0101685650636456,
 2.157929841542483,
 0.33568062217888556,
 0.38316226737466064,
 -0.2702564539926545,
 1.8844589670060572,
 1.122127737998427,
 -0.6914810743395978,
 -0.35617586101462395,
 1.392435250473011,
 1.0384235082141562,
 -0.43919995880131746,
 0.11900287818300977,
 -1.1477900033424273,
 1.5268675220042884,
 1.0307816258603195,
 -0.7256445847752935,
 -0.20446756470268382,
 0.2424837105375871,
 -0.46272805728056815,
 -0.27438960511569876,
 0.8255174964868129,
 -0.32832347556790586,
 -0.8047244652526225,
 -1.8975178763415805,
 -1.043893542107416,
 0.6215185321300284,
 0.050477422085883614,
 -0.8006534281147186,
 0.8208867267087376,
 -0.646690399889707,
 1.0146037349111148,
 0.3458440694581085,
 0.25938806269362175,
 0.34881794305333946,
 1.9007203014084857,
 1.4580216822483594,
 -1.1010769416191484,
 0.48396460633924065,
 -0.8620683559285559,
 -1.1111370808833665,
 -0.22716296800999772,
 0.043245642949724626,
 -1.090761777567797,
 -2.02026688356726,
 0.5120732747452219,
 0.9507196688774703,
 -2.059045592999979,
 -0.39590361044092187,
 0.05123227266500733,
 -0.9923160778491765,
 1.16083980287336,
 0.4891319196186199,
 -0.4060635305390198,
 0.08653087442334469,
 -1.0109485260118685,
 -0.36521675020420685,
 -1.4185351284562489,
 1.05150581244885,
 -2.4478372496681136,
 1.0783701078811865,
 -1.3579589266668068,
 0.2895785360039318,
 1.953188682990177,
 1.3930583016606937,
 1.4875390149444003,
 -0.8194623002855265,
 -2.101219274369832,
 0.6160132363690541,
 0.3273291725794887,
 0.03291549904800823,
 -0.713789374125793,
 1.958167557084519,
 1.5752095635121133,
 -0.678532752258295,
 -0.05760426866602336,
 1.743943745129652,
 1.615588111605621,
 1.516338486494783,
 0.6703361772773315,
 0.025698639670598045,
 -0.8986169524221697,
 0.6141561828144725,
 -0.33714256787787367,
 1.4470900025482274,
 -1.0351381231283567,
 -1.0944520444753112,
 1.476256642750221,
 1.2410788080290183,
 -1.8050958674954736,
 0.3283993875957449,
 -0.1094669296431455,
 0.7744087976660289,
 1.229770811290647,
 -0.15802995674499104,
 -1.1656692675210023,
 -1.5235380802283862,
 -0.7196764000422007,
 -1.89620608241246,
 -1.0136762042093446,
 0.6016198681217595,
 2.8596663951535866,
 -0.36147597752196564,
 1.7817333766661678,
 -0.44268694837004047,
 -0.10518155429890537,
 0.18833175481758352,
 0.1344689825064891,
 -1.7553554555084014,
 -0.04628131068006171,
 -0.5555585540132062,
 0.6186001872077052,
 -0.9552766399095695,
 0.38054288495522204,
 0.12369153876956338,
 0.26653728076456423,
 -1.0469931683055662,
 0.9879683237029497,
 0.24672882913098837,
 1.73785048222997,
 2.079137403493711,
 -0.20164161386456692,
 1.5729443586523761,
 1.3065245106974817,
 0.15391877708388604,
 -1.991933455154586,
 -0.7832621100600697,
 -0.29390291962134507,
 -1.1931187967587666,
 2.232491920791487,
 -0.43534108514156283,
 2.0306640986270397,
 -0.3836650825753771,
 0.9344273175441424,
 -0.20611290533218546,
 0.07573631210744201,
 -0.2742128130198334,
 1.0518042512348353,
 0.2097338222578382,
 0.5181352771730925,
 1.2499531818961016,
 -1.9409624810163146,
 -0.5250516060933302,
 -1.1001609509543395,
 1.8539684249510933,
 -0.014872077421318545,
 0.9407834103818214,
 0.7313751339674799,
 -0.6983166096788272,
 -1.078499062309666,
 1.3788072793104658,
 1.74039060998762,
 -1.1394734094921937,
 -0.7287405621619073,
 1.906734365693572,
 -0.9671526942528912,
 0.1943855335178476,
 0.08369853605512177,
 -0.11843486706265952,
 0.19021885922085605,
 1.0173239136780334,
 -0.059761776419942125,
 1.0768434138916803,
 -1.742245977090785,
 -0.14287381467437377,
 0.7887973715957904,
 -0.71522216042308,
 -0.40450006666377053,
 0.39577418155254923,
 -1.5620912621287524,
 -0.7600154949796959,
 0.5504669957612354,
 -0.9715650829457404,
 -0.7358438519423103,
 -1.3391939989250805,
 0.13055805011025223,
 0.18034950198520597,
 -1.587874427219796,
 -0.009144236997897282,
 2.109617397658865,
 -0.8179418991471744,
 -0.8533464242624935,
 1.6231394424967276,
 1.1731110560707163,
 2.2055381419111204,
 1.0080203530972165,
 0.8678837315684362,
 -0.15387300458140854,
 -0.5964482678473142,
 -1.2512423831290906,
 -1.3966551919556884,
 -1.3444085779040635,
 1.918066015362548,
 1.0004490940434345,
 0.08326606220768627,
 -0.7510944351806857,
 0.20273454053289527,
 0.6507229245968343,
 -0.9064253250420312,
 0.055596082954440455,
 -0.9085918711665725,
 1.3622918768486314,
 -0.5264921902165512,
 -0.15878905333756874,
 0.30558386268697996,
 0.06148036910322185,
 1.1174037011628208,
 -0.6614073957749507,
 -0.5462321671845819,
 1.27192158847077,
 -1.3533732370708214,
 0.35060479839281233,
 -2.4496998403811117,
 -0.9110331685715535,
 -0.17942978498517914,
 0.5182779795274043,
 -0.8331381692118263,
 0.044644875808767144,
 -0.428083358327155,
 -1.9568601490236512,
 0.9266494076723562,
 0.7605496545279905,
 -0.4746800801017311,
 -0.7487931416988324,
 -0.7812977531949366,
 -0.3809915246236244,
 0.7429744249580562,
 -0.965938717427877,
 -0.9049096579776756,
 0.7354385487257387,
 1.1666252152169723,
 -0.21593746161203142,
 -0.5914168304378818,
 0.7849803761058523,
 -0.09083772258614078,
 0.19152976699543647,
 2.565537107378648,
 0.7474284630510472,
 -0.3473007050911652,
 0.4851824258159357,
 0.7614297362936089,
 0.1299339601457267,
 -0.26019184562718584,
 0.4029297445222532,
 -1.2148297807924393,
 1.7841167805541536,
 -0.852447496661363,
 1.1538294668342466,
 0.013635530300940626,
 0.9094453762027831,
 -0.7783546761479777,
 -0.5115051782219592,
 0.3971389245651492,
 0.17334136854941626,
 -0.8060806716660475,
 1.3136571453034096,
 -1.2897214259458247,
 2.4879057501198334,
 0.8134761735245407,
 -0.9420000760340741,
 0.8659167120242292,
 -0.617480376270238,
 -0.5415639943607561,
 -1.5331596501006852,
 1.026459550555951,
 -0.09205473362816607,
 -1.0768449423991115,
 0.8874209899693404,
 0.3784309709018792,
 -0.9859716467757411,
 -0.9727911358022753,
 -1.9125151631264832,
 -1.4767606079092417,
 -0.025691049511636945,
 -0.39050283086103504,
 0.6192880473807181,
 -0.6444859458514633,
 0.7466349356265559,
 -1.2882770580393352,
 0.024677338969325502,
 0.13313963452232738,
 -0.5599948872309307,
 -0.5748615305676719,
 -0.1316634899037439,
 1.5093564897192262,
 0.46466426962609475,
 -0.07098264626298201,
 0.050203021770624766,
 -0.0770551775401295,
 -1.723227311315135,
 1.2187279234672699,
 -1.8131609106088975,
 -1.1266515368067,
 1.8558095453794654,
 0.7344049627249823,
 -2.204254479217442,
 0.3052713666532666,
 1.1809151937371627,
 -0.39213418125957106,
 -0.550834061460781,
 ...]

In [56]:
# and print our statistics
print_statistics(gaussValues, calc_mode=False)


Count: 20000
Mean: -0.01
Median: 0.0035974390206229565
Max: 4.291088338639748
Min: -4.52593184929381
Range: 8.817020187933558
25th Percentile: -0.6838808785947806
50th Percentile: 0.003600379797408655
75th Percentile: 0.665650740246061
IQR: 1.3495316188408415

The nature of the function is such that the mean should fall around 0. It looks like we accompished that. Also note how the 25th and 50th percentile are roughly the same number. This is an indication that the distribution is not significantly skewed. Let's take a look at it's histogram:


In [57]:
sns.distplot(gaussValues, kde=False)


Out[57]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fee23085b00>

Get used to this image because you will see it everywhere in statistics, data science, and life. Even though there are many many distributions out there (and even more variations on each of those), most people will be happy to apply the "bell curve" to almost anything. Chance are, though, they're right.

Variability

Let's talk about one more descriptive statistic that describes to us how much the values vary. In other words, if we're looking at test scores, did everyone do about the same? Or were there some big winners and losers?

Mathemeticians call this the variability. There are three major measures of variability: range, inter-quartile range (IQR), and variation/standard devaition.

1. Range

Range is a very simple measure: how far apart could my values possibly be? In our generated datasets above, the answer was pretty obvious. We generated a random number from 0 to 1000, so the range was $1000-0 = 1000$. The values of x could never go outside of this range.

But what about our gaussian values? The tandard normal distribution has asymptotic end points instead of absolute end points, so it's not so clean. We can see from the graph above that it doesn't look like there are any values above and below 4, so we'd expect a range of something around 8:


In [58]:
print("Max: {}\nMin: {}\nRange: {}".format(
    max(gaussValues),
    min(gaussValues),
    max(gaussValues) - min(gaussValues)))


Max: 4.291088338639748
Min: -4.52593184929381
Range: 8.817020187933558

Exactly what we expected. In practice, the range is a good descriptive statistic, but you can't do many other interesting things with it. It basically let's you say "our results were between X and Y", but nothing too much more profound.

Another good way to describe the range is called

2. Inter-Quartile Range

or IQR for short. This is a similar technique where instead of taking the difference between the max and min values, we take the difference between the 75th and 25th percentile. It gives a good sense of the range because it excludes outliers and tells you where the middle 50% of the values are grouped. Of course, this is most useful when we're looking at a unimodal distribution like our normal distribution, because for a distribution that's bimodal (i.e. has many values at either end of the range), it will be misleading.

Here's how we caluclated it:


In [59]:
print("75th: {}\n25th: {}\nIQR: {}".format(
    pct_value(gaussValues, .75),
    pct_value(gaussValues, .25),
    pct_value(gaussValues, .75) - pct_value(gaussValues, .25)
))


75th: 0.665650740246061
25th: -0.6838808785947806
IQR: 1.3495316188408415

So again, this tells us that 50% of our values are between -0.68 and 0.68 and all within the same 1.35 values. Comparing this to the range (which is not at all influenced by percentages of values), this gives you the sense that the're bunched around the mean.

If we want a little more predictive power, though, it's time to talk about

3. Variance

Variance is a measure of how much values typically deviate (i.e. how far away they are) from the mean.

If we want to calculate the variance, then we first see how far a value is from the mean ($\mu$), square it (which gets rid of the negative), sum them up, and divide by $n$. Essentially, it's an average where the value is the deviation squared instead of the value itself. Here's the formula for variance, denoted by $\sigma^2$:

$$ \sigma^2 = \frac{(x_1 - \mu)^2+(x_2 - \mu)^2+ \cdots + (x_n - \mu)^2}{n} $$

Let's code that up:


In [61]:
def variance(vals):
    n = len(vals)
    m = arithmetic_mean(vals)
    variance = 0
    for x in vals:
        variance += (x - m)**2
    return variance/n

variance(gaussValues)


Out[61]:
1.0064079218311726

The variance four our generated Gaussian numbers is roughly 1, which is another condition of a standard normal distribution (mean is 0, variance is 1). So no surprise. But the variance is a bit of a tricky number to intuit because it is the average of the squared differences between the mean. Take our skewed for example:


In [62]:
variance(skewed)


Out[62]:
918.4940092609672

How do you interpret this? The max value is 100, so is a variance of 934.8 a lot or a little? Also, let's say the skewed distribution was a measure of price in dollars. Therefore, the units of variance would be 934.8 dollars squared. Doesn't make a whole lot of sense.

For this reason, most people will take the square root of the variance to give them a value called the standard deviation: $\sigma = \sqrt{\sigma^2}$.


In [63]:
def stddev(vals):
    return sqrt(variance(vals))
stddev(skewed)


Out[63]:
30.306666086208942

This is much more approchable. It's saying the standard devaition from our mean is \$30. That is an easy number to digest. This is a very important number to be able to grok. I'll repeat it to drive the point home:

The standard deviation is a measure of how "spread out" a distribution is. The higher the value, the further a typical observation from our population is from the mean of that population.

The standard devation, range, and IQR all give measures of this dispersion of a distribution. However, for reasons we will see later, the standard devaition is the work horse of them all. By knowing the standard deviation of a population (or sample, as the case often is), we can begin to judge how likely a value is to get, or how sure we are of our own guesses. Statistics is a glorified guessing game, and stddev is one of the most important tools.

Next time

Some of the topics to look forward to are

  • Paired data
    • Scatter plots
    • Explanatory vs response variables
    • Covariance and correlation
  • Random Variables and Probability
  • Distributions
    • Discrete vs Continuous
    • Descriptive statistics of distributions
    • Major types of distributions
      • Normal
      • Geometric
      • Binomial
      • Poisson
      • Chi-squared ($\chi^2$)
      • Weibull
  • Inferential statistics
    • Sampling distributions and statistics
    • Central limit theorem
    • Hypothesis testing

Useful Resources

The git repository for this notebook (and all notebooks) is found here. Feel free to submit a pull request or issue there, or download and work with the notebook yourself.

I found these resources particularly useful in compiling this notebook:

Found others? Drop us a line. Until next time.